#include "FileDedupe.h"

#include <algorithm>

FileDedupe::FileDedupe(const std::shared_ptr<FilesystemReader>& fsreader, 
                       const CompositeFilter& filter, 
                       const std::shared_ptr<SelectorAlgorithm>& selector_alg,
                       const std::shared_ptr<Executor>& executor) : 
   
                       fsreader_(fsreader),
                       filter_(filter),
                       selector_alg_(selector_alg),
                       executor_(executor) { }

FilePairsType FileDedupe::generatePairs() {

    FilePairsType pairs;
    auto end = files_.end();

    for (auto iter_first = files_.begin(); iter_first != end; ++iter_first) {
        for (auto iter_sec = iter_first; ++iter_sec != end; ) {
            auto file_pair = std::make_pair(&(*iter_first), &(*iter_sec));
            pairs.push_back(file_pair);
        }
    }
    return pairs;
}

void FileDedupe::process() {

    files_ = fsreader_->readFiles();
    input_pairs_ = generatePairs(); 

    input_pairs_ = executor_->run(
        [this](const FilePairType& arg) {
            return this->filter_(arg);
        }, input_pairs_);

    executor_->run(
        [this](const FilePairType& arg) {
            return AlgResultType(
                   this->selector_alg_->getSim(arg)
                   );
        }, input_pairs_);
}


